package main

import (
	"fmt"
	"time"
)

func m1(controlFlag *ControlFlag) {
	for i := 0; i < 10; i++ {
		if controlFlag.Stop {
			return
		}
		time.Sleep(1000 * time.Millisecond)
		fmt.Println("m1: ", i)
	}
}

func m2(controlFlag *ControlFlag) {
	for i := 0; i < 20; i++ {
		if controlFlag.Stop {
			return
		}
		time.Sleep(1000 * time.Millisecond)
		fmt.Println("m2: ", i)
	}
}

func m3(controlFlag *ControlFlag) {
	if controlFlag.Stop {
		return
	}
	for i := 0; i < 30; i++ {
		time.Sleep(1000 * time.Millisecond)
		fmt.Println("m3: ", i)
	}
}

type ControlFlag struct {
	Stop bool
}

func main() {
	controlFlag := &ControlFlag{
		Stop: false,
	}
	go func(controlFlag *ControlFlag) {
		m1(controlFlag)
		m2(controlFlag)
		m3(controlFlag)
	}(controlFlag)

	time.Sleep(3 * time.Second)
	controlFlag.Stop = true

	time.Sleep(100 * time.Second)
}
